package cn.com.gridinfo.sooc.core.expand.signin.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;

import java.util.List;

import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.core.expand.callname.bean.ClassInfo;

/**
 * 今日签到界面的列表的适配器
 *
 * @author yingchunli
 */
public class TodaySignAdapter extends UltimateViewAdapter<TodaySignAdapter.ViewHolder> {

    public List<ClassInfo> classInfoList;
    private Context context;
    private View viewParent;//布局

    public TodaySignAdapter(Context context, List<ClassInfo> classInfoList) {
        this.context = context;
        this.classInfoList = classInfoList;
    }

    @Override
    public ViewHolder getViewHolder(View view) {
        return new ViewHolder(view, false);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent) {
        //加载布局
        viewParent = LayoutInflater.from(parent.getContext()).inflate(R.layout.expand_sign_todaysign_item, parent, false);
        ViewHolder holder = new ViewHolder(viewParent, true);
        viewParent.setOnClickListener(holder);
        return holder;
    }

    @Override
    public int getAdapterItemCount() {
        return classInfoList.size();
    }

    @Override
    public long generateHeaderId(int position) {
        return position;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        if (position < getItemCount() &&
                (customHeaderView != null ? position <= classInfoList.size() : position < classInfoList.size())
                && (customHeaderView != null ? position > 0 : true)) {
            ClassInfo classInfo = classInfoList.get(position);
            holder.kcmc.setText(classInfo.getKcmc());
            holder.ktbh.setText("" + classInfo.getKtbh());
            holder.lsmc.setText(classInfo.getLsmc());
            holder.ktzt.setText(classInfo.getKtzt());
            if (classInfo.getQdzt().equals("0")) {
                holder.sign.setText("签到");
                holder.sign.setVisibility(View.VISIBLE);
                holder.yiqdIcon.setVisibility(View.GONE);
            } else {
                holder.sign.setText("已签到");
                holder.sign.setVisibility(View.GONE);
                holder.yiqdIcon.setVisibility(View.VISIBLE);
            }
            //底部分割线的显示
//            if (position == classInfoList.size() - 1) {
//                holder.line.setVisibility(View.VISIBLE);
//            } else {
//                holder.line.setVisibility(View.GONE);
//            }
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        return null;
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
    }

    /**
     * 清空数据
     */
    public void clear(){
        classInfoList.clear();
    }

    /**
     * 点击事件接口
     */
    public interface OnItemClickListener {
        void OnItemClick(View view, ClassInfo classInfo);
    }

    private OnItemClickListener mOnItemClickLitener;

    public void setmOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    public void loadmore(List<ClassInfo> list) {
        classInfoList.addAll(list);
        notifyDataSetChanged();
    }

    public void refresh(List<ClassInfo> list) {
        clear(classInfoList);
        classInfoList.addAll(list);
        notifyDataSetChanged();
    }

    //ViewHolder
    public class ViewHolder extends UltimateRecyclerviewViewHolder implements View.OnClickListener {
        TextView ktbh, kcmc, lsmc, ktzt;
        TextView sign;//未签到的显示文字
        ImageView yiqdIcon;//已签到的显示图片
//        View line;

        public ViewHolder(View itemView, boolean isItem) {
            super(itemView);
            if (isItem) {
                ktbh = (TextView) itemView.findViewById(R.id.ktbh_todaysign);
                kcmc = (TextView) itemView.findViewById(R.id.kcmc_todaysign);
                lsmc = (TextView) itemView.findViewById(R.id.lsmc_todaysign);
                ktzt = (TextView) itemView.findViewById(R.id.ktzt_todaysign);
                sign = (TextView) itemView.findViewById(R.id.sign_todaysign);//签到
                yiqdIcon = (ImageView) itemView.findViewById(R.id.yiqd_icon);
//                line = (View) itemView.findViewById(R.id.line_bottom);//底部分割线
            }
        }

        @Override
        public void onClick(View v) {
            if (classInfoList.get(getAdapterPosition()).getQdzt().equals("0"))
                //签到动作
                mOnItemClickLitener.OnItemClick(v, classInfoList.get(getAdapterPosition()));
        }
    }

}
